package pratice.Jbarcode;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

/**
 * Zxing可以支持EAN13条形码
 *
 * @blog https://gaozzsoft.iteye.com/blog/1617392; https://blog.csdn.net/zmx729618/article/details/52846546
 * @author 吴宇亮 on 2019/6/12 0012 上午 11:00
 */
public class JarBarLearn {

    public void encodeEAN13(String contents, int width, int height, String imgPath) {
        int codeWidth = 3 + // start guard
                (7 * 6) + // left bars
                5 + // middle guard
                (7 * 6) + // right bars
                3; // end guard
        codeWidth = Math.max(codeWidth, width);
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.EAN_13, codeWidth, height, null);
            MatrixToImageWriter.writeToFile(bitMatrix, "png", new File(imgPath));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void encodeEAN13Test() {
        String imgPath = "d:/test/twocode/zxing_EAN13.png";
        // 益达无糖口香糖的条形码
        String contents = "6923450657713";

        int width = 105, height = 50;
        JarBarLearn handler = new JarBarLearn();
        handler.encodeEAN13(contents, width, height, imgPath);

        System.out.println("Michael ,you have finished zxing EAN13 encode.");
    }

    public String decodeEAN13(String imgPath) {
        BufferedImage image = null;
        Result result = null;
        try {
            image = ImageIO.read(new File(imgPath));
            if (image == null) {
                System.out.println("the decode image may be not exit.");
            }
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
            result = new MultiFormatReader().decode(bitmap, null);
            return result.getText();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Test
    public void decodeEAN13Test() {
        String imgPath = "F:/zxing_EAN13.jpg";
        JarBarLearn handler = new JarBarLearn();
        String decodeContent = handler.decodeEAN13(imgPath);
        System.out.println("解码内容如下：");
        System.out.println(decodeContent);
        System.out.println("Michael ,you have finished zxing EAN-13 decode.");

    }

    /*public void encodeEAN8(String content, String imgPath, int width, int height) {
        try {
            JBarcode jBarcode = new JBarcode(EAN8Encoder.getInstance(),
            WidthCodedPainter.getInstance(), EAN8TextPainter.getInstance());
            BufferedImage bufferedImage = jBarcode.createBarcode(content);
            FileOutputStream outputStream = new FileOutputStream(imgPath);
            ImageUtil.encodeAndWrite(bufferedImage, "jpeg", outputStream, width,height);
            outputStream.close();
            System.out.println("===");
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

    @ControllerTest
    public void encodeEAN8Test(){
        JarBarLearn EAN8 = new JarBarLearn();
        EAN8.encodeEAN8("1101111", "F:/a.jpg", 100, 70);
    }*/
}
